﻿using System;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using ZilLion.Service.Core.Infrastructure.Middleware.WebSocket.Interface;

namespace ZilLion.Service.Core.Infrastructure.Middleware.WebSocket
{
    public static class WebSocketManagerExtensions
    {
        public static IServiceCollection AddWebSocketManager(this IServiceCollection services)
        {
            services.TryAddSingleton<WebSocketConnectionManager>();//注册单例
            services.TryAddSingleton<IMessageHandleStrategyFactory,MessageHandleStrategyFactory>();//注册策略工厂单例
            services.TryAddSingleton<IWebSocketHandler, WebSocketHandler>();//注册策略工厂单例
           
            return services;
        }

        public static IApplicationBuilder MapWebSocketManager(this IApplicationBuilder app,IServiceProvider serviceProvider)
        {
            return app.Map("/websocket", (_app) => _app.UseMiddleware<WebSocketManagerMiddleware>(serviceProvider.GetService<IWebSocketHandler>()));
        }
    }
}
